package com.visikard.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.visikard.ui.R;
import com.visikard.ui.fragment.RedeemptionFragment;
import com.visikard.ui.fragment.ViewClickable;

public class RedemptionActivity extends BaseLoginCompleteActivity implements ViewClickable {
	public static final int MSG_OPEN_TRASACTIONS_DEATIL = 4;
	public static final String TAG_CONTENT_FRAGMENT = "RedeemptionCodeMainActivity_tag_left_content_fragmetn";
	public static final String KEY_STARTUP_FRAGMENT = "key_startup_fragment";
	public enum RedeemptionActivityEnum{
		SCANCODE, TYPECODE;
	}
		
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.redeemption_activity);
		
		Fragment contentFrag = new RedeemptionFragment();
		Bundle args = new Bundle();
		//get the start up fragment
		RedeemptionActivityEnum startupFrag = RedeemptionActivityEnum.SCANCODE;
		if(getIntent().getSerializableExtra(KEY_STARTUP_FRAGMENT) != null){
			startupFrag = (RedeemptionActivityEnum)getIntent().getSerializableExtra(KEY_STARTUP_FRAGMENT);
		}
		switch(startupFrag){
		case SCANCODE:
			args.putSerializable(KEY_STARTUP_FRAGMENT, RedeemptionActivityEnum.SCANCODE);
			break;
		case TYPECODE:
			args.putSerializable(KEY_STARTUP_FRAGMENT, RedeemptionActivityEnum.TYPECODE);
			break;
		default:			
			args.putSerializable(KEY_STARTUP_FRAGMENT, RedeemptionActivityEnum.SCANCODE);
		}
		contentFrag.setArguments(args);
		
		setHomeContent(contentFrag);
	}
	
	public void setHomeContent( Fragment contentFragment) {		
		getSupportFragmentManager().beginTransaction().replace(R.id.main_content, contentFragment, TAG_CONTENT_FRAGMENT).commit();		
	}
	
	public void mainPutFragment(Fragment fm, String tab){
		((RedeemptionFragment)getHomeFragment()).removeFragment();
		((RedeemptionFragment)getHomeFragment()).putFragment(fm, tab);
	}
	
	public Fragment getHomeFragment() {
		return getSupportFragmentManager().findFragmentByTag(TAG_CONTENT_FRAGMENT);
	}
	
	@Override
	public void onViewClick( View v ) {
		if(v.getId() == R.id.home_btn_menu){
			finish();
			return;
		}
		
		Object contentFragment = getHomeFragment();
		
		if( contentFragment != null && (contentFragment instanceof ViewClickable) ) {
			((ViewClickable)contentFragment).onViewClick(v);
		}
	}
	
	@Override
	public void onBackPressed() {
		finish();
	}
	
}
